// Written by Steve Andersen, copyright (c) 2007 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/
public class TestGivingHistory {

	private static string GiftRectype = Constants.OPP_DEFAULT_RECTYPE_FORTESTS;

	// TBD test bulk

	// TBD test undelete
	
	// TBD test changing close date to different year
	
	// TBD test non-gift opp rectype
	
	// TBD test account rollup
	// TBD test changing from one acct to another?


	static testMethod void OneContactMultipleOpps() {

		integer howMany = 1;
		Date datToday = System.Today();
		Date dat1YearAgo = Date.newInstance( datToday.year()-1,1,1);
		Date dat2YearAgo = Date.newInstance( datToday.year()-2,1,1);
		Date dat4YearAgo = Date.newInstance( datToday.year()-4,1,1);
			
		// create & insert contact(s)
		Contact[] TestCons = UnitTestData.CreateMultipleTestContacts ( howMany ) ;
		insert TestCons;
		
		system.debug ( 'TEST>>>>> inserting gift 1...');
		
		// create a new gift for this yr
		Opportunity[] testGift1 = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', datToday, 100 , GiftRectype);
		insert testGift1 ;
		
		// reset the conrole creation flag so we'll get conroles for this 2nd insert
		OppContactRoles.haveCheckedContactRoles = false;

		system.debug ( 'TEST>>>>> inserting gift 2...');
		
		//create a 2nd gift for last yr
		Opportunity[] testGift2 = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', dat1YearAgo, 60, GiftRectype);
		OppContactRoles.haveCheckedContactRoles = false;
		insert testGift2;
		
		//now test that the contact has received the proper stats from the trigger
		id ThisConId = TestCons[0].id;
		Contact UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 160 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 100 , UpdatedCon.Total_Gifts_YTD__c );
		System.AssertEquals ( 60 , UpdatedCon.Total_Gifts_Last_CY__c );


		system.debug ( 'TEST>>>>> changing gift 1...');
		
		// now chg the amts for both opps (cheapskate!)
		testGift1[0].Amount = 50;
		update TestGift1;

		system.debug ( 'TEST>>>>> changing gift 2...');
		
		testGift2[0].Amount=25;
		update TestGift2;
		
		//now test that the contact has updated stats from the trigger
		ThisConId = TestCons[0].id;
		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 75 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 50 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		

		system.debug ( 'TEST>>>>> inserting gift 3...');

		// now create a gift from 2 yrs ago
		Opportunity[] testGift3 = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', dat2YearAgo, 10 , GiftRectype);
		OppContactRoles.haveCheckedContactRoles = false;
		insert testGift3;

		//now test that the contact has updated stats from the trigger
		ThisConId = TestCons[0].id;
		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 85 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 50 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		System.AssertEquals ( 10 , UpdatedCon.Total_Gifts_2_CY_Ago__c );

		// add another from this year (to test adding)
		system.debug ( 'TEST>>>>> inserting gift 4...');
		Opportunity[] testGift4 = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', datToday, 25 , GiftRectype);
		OppContactRoles.haveCheckedContactRoles = false;
		insert testGift4;

		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 110 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 75 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		System.AssertEquals ( 10 , UpdatedCon.Total_Gifts_2_CY_Ago__c );


		// TBD add a gift from longer ago
		system.debug ( 'TEST>>>>> inserting gift 5...');
		Opportunity[] testGift5 = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', dat4YearAgo, 200 , GiftRectype);
		OppContactRoles.haveCheckedContactRoles = false;
		insert testGift5;
		
		// totals should not have changed, except lifetime
		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 310 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 75 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		System.AssertEquals ( 10 , UpdatedCon.Total_Gifts_2_CY_Ago__c );
		
		
		// TBD add non-won gift
		system.debug ( 'TEST>>>>> inserting gift 6...');
		Opportunity[] testGift6 = UnitTestData.OppsForContactList ( TestCons, null, 'Prospecting', dat4YearAgo, 35 , GiftRectype);
		OppContactRoles.haveCheckedContactRoles = false;
		insert testGift6;
		
		// totals should not have changed at all
		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 310 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 75 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		System.AssertEquals ( 10 , UpdatedCon.Total_Gifts_2_CY_Ago__c );
		
		
		// now delete the 1st gift (now at $50), totals should decrease
		system.debug ( 'TEST>>>>> deleting gift 1...');	
		delete testGift1;
		
		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 260 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		System.AssertEquals ( 10 , UpdatedCon.Total_Gifts_2_CY_Ago__c );
		
	}




/* commented out for now 11/1/07 MMS - this was old test built to test old non-bulk trigger, will probably delete
	static testMethod void TestContacts() {
		//create a contact
		Contact firstContact = new Contact (
			FirstName='Joe',
			LastName='Johanssen',
			MailingStreet='123 Elm St',
			MailingCity='Wauwatosa',
			MailingState='WI',
			MailingPostalCode='53213',
			MailingCountry='USA'			
		);
		insert firstContact;
		//get that contact
		Contact[] firstcontact1 = [select ONEN_Household__c from Contact where Id =:firstContact.id];
		//create a second contact
		Contact secondContact = new Contact (
			FirstName='Larry',
			LastName='Johanssen',
			ONEN_Household__c=firstcontact1[0].ONEN_Household__c		
		);
		insert secondContact;
				
		//create a gift
		Opportunity opp1 = new Opportunity (
			Name='Joe Johanssen 2005 Gift',
			CloseDate=Date.newInstance(2005,10,10),
			StageName='Closed Won',
			Amount=100						
		);
		insert opp1;
		//create a contact role for that gift and contact
		OpportunityContactRole cr1 = new OpportunityContactRole (
			OpportunityId=opp1.id,
			ContactId=firstContact.id,
			Role='Individual Donor',
			IsPrimary=true						
		);
		insert cr1;
		//modify the opp
		Opportunity opp20 = new Opportunity (
			id=opp1.id,
			Amount=200						
		);
		update opp20;
		
		//2005 giving total should be updated to reflect the value of this gift
		//Contact[] contact1 = [select id,Lifetime_Value__c,FY2005__c,FY2006__c from Contact where Id =:firstContact.id];
		//System.assertEquals(200,contact1[0].FY2005__c);
		
				
		
		//create another opportunity with the contact role attached
		Opportunity opp4 = new Opportunity (
			Name='Test Opp 2',
			CloseDate=Date.newInstance(2006,10,10),
			StageName='Closed Won',
			Amount=170,
			CR_Contact_Id__c=firstcontact.id,
			CR_Contact_Role__c='Individual Donor'			
		);
		insert opp4;
		
		//check to see that total has incremented
		//Contact[] contact2 = [select id,Lifetime_Value__c,FY2005__c,FY2006__c from Contact where Id =:firstContact.id];
		
		//System.assertEquals(370,contact2[0].Lifetime_Value__c);
		//System.assertEquals(200,contact2[0].FY2005__c);
		//System.assertEquals(170,contact2[0].FY2006__c);
		//delete the first opp
		Opportunity opp3 = new Opportunity (
			id=	opp1.id				
		);
		delete opp3;
		//check to see that total has decremented
		Contact[] contact4 = [select id,Lifetime_Value__c from Contact where Id =:firstContact.id];
		//System.assertEquals(170,contact4[0].Lifetime_Value__c);
		
		Opportunity oppCR = new Opportunity (
			Name='Test Opp 1',
			CloseDate=Date.newInstance(2007,6,6),
			StageName='Closed Won',
			Amount=1000,
			CR_Contact_Id__c=firstcontact.id,
			CR_Contact_Role__c='Individual Donor'						
		);
		insert oppCR;
		System.assertEquals(2,[select count() from OpportunityContactRole where OpportunityId = :oppCR.id]);
		Contact[] contact5 = [select id,Lifetime_Value__c from Contact where Id =:firstContact.id];
	//	System.assertEquals(1170,contact5[0].Lifetime_Value__c);	
	} */

}